package com.ikun.service.impl;

import com.ikun.mapper.TbTaskMapper;
import com.ikun.pojo.TbTask;
import com.ikun.pojo.TbType;
import com.ikun.pojo.TbUser;
import com.ikun.pojo.TbUserTask;
import com.ikun.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    TbTaskMapper taskMapper;

    //分页查询任务的方法
    @Override
    public List<TbTask> pgSelTask(int page,String order,String by) {
        int pg = (page-1)*30;
        List<TbTask> tbTaskList = taskMapper.pgSelTask(pg,order,by);
        return tbTaskList;
    }

    @Override
    public int send_task(String title, long typeId, String content, long userid,int cardnum,int usernum) {
        TbTask tbTask = new TbTask();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        TbType tbType = new TbType();
        tbType.setId(typeId);
        TbUser tbUser = new TbUser();
        tbUser.setId(userid);
        // 返回系统当前时间
        String createTime = df.format(new Date());

        int grade =0;
        tbTask.setTitle(title);
        tbTask.setType(tbType);
        tbTask.setUser(tbUser);
        tbTask.setContent(content);
        tbTask.setCardnum(cardnum);
        tbTask.setGrade(grade);
        tbTask.setCreatetime(createTime);
        tbTask.setUsernum(usernum);
        int count = taskMapper.send_task(tbTask);

        System.out.println("完成行数:"+count);
        return count;
    }

    //查询我发布的任务的方法
    @Override
    public List<TbTask> selMyTask(int userid, int page) {
        int pg = (page-1)*8;//limit 0,8
        List<TbTask> taskList = taskMapper.selMyTask(userid,pg);
        return taskList;
    }

    //查询我发布的任务的数量的方法
    @Override
    public int selMyTaskCount(int userid) {
        int count = taskMapper.selMyTaskCount(userid);
        if (count <= 8){
            count = 1;
        }else{
            if (count%8 == 0){
                count = count/8;
            }else{
                count = count/8+1;
            }
        }
        System.out.println(count);
        return count;
    }

    //查询我参与的任务的方法
    @Override
    public List<TbTask> selMyJoin(int userid, int page) {
        int pg = (page-1)*8;
        List<TbTask> taskList = taskMapper.selMyJoin(userid,pg);
        return taskList;
    }

    //查询我参与的任务的数量的方法
    @Override
    public int selMyJoinCount(int userid) {
        int count = taskMapper.selMyJoinCount(userid);
        if (count <= 8){
            count = 1;
        }else{
            if (count%8 == 0){
                count = count/8;
            }else{
                count = count/8+1;
            }
        }
        return count;
    }

    @Override
    public TbTask selTaskInfo(int userid) {
        TbTask tbTask = taskMapper.selTaskInfo(userid);
        return tbTask;
    }

    @Override
    public int getTask(TbUserTask tbUserTask) {
        int count =  taskMapper.getTask(tbUserTask);
        return count;
    }
}
